package Generic;

import java.util.ArrayList;

/**
 * @author 王艺锡
 * @version 1.0
 * 引出泛型
 */
@SuppressWarnings({"all"})
public class GenericIntroduce {
    public static void main(String[] args) {
        ArrayList arrayList = new ArrayList();
        arrayList.add(new Dog("小黄",1));
        arrayList.add(new Dog("小黑",2));
        arrayList.add(new Dog("小白",3));

        //加入不小心添加了一只猫,会导致下边程序错误
        arrayList.add(new Cat("泡芙",3));


        for (Object o :arrayList) {
            Dog dog = (Dog) o;
            System.out.println(dog.getName() + "_" + dog.getAge() + "岁");
        }


    }
}
class Dog{
    private String name;
    private int age;

    public Dog(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

class Cat{
    private String name;
    private int age;

    public Cat(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
